package cn.bytepath.systemdictionary.service.impl;

import cn.bytepath.base.service.impl.BaseServiceImpl;
import cn.bytepath.systemdictionary.domain.SystemDictionaryDetail;
import cn.bytepath.systemdictionary.domain.SystemDictionaryType;
import cn.bytepath.systemdictionary.mapper.SystemDictionaryDetailMapper;
import cn.bytepath.systemdictionary.mapper.SystemDictionaryTypeMapper;
import cn.bytepath.systemdictionary.service.ISystemDictionaryDetailService;
import cn.bytepath.systemdictionary.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 产品字典分类对应的业务层实现类
 * @author Jack.C
 * @createDate 2022-02-12
 * @version 1.0
 */
@Service
public class SystemDictionaryTypeServiceImpl extends BaseServiceImpl<SystemDictionaryType> implements ISystemDictionaryTypeService {

    /**
     * 注入字典类型mapper对象
     */
    @Autowired
    private SystemDictionaryTypeMapper systemDictionaryTypeMapper;

    /**
     * 注入字典详细mapper对象
     */
    @Autowired
    private SystemDictionaryDetailMapper systemDictionaryDetailMapper;

    /**
     * 删除单条产品类型字典中的记录,若该记录被引用成外键则需先删除其引用的记录
     * @param id
     * @update 2022/2/12
     */
    @Override
    public void delete(Long id) {
        Long[] ids = systemDictionaryTypeMapper.findAllDetailIdById(id);
        System.out.println(Arrays.toString(ids));
        if(ids.length == 0) {
            super.delete(id);
        } else {
            systemDictionaryDetailMapper.deleteBatch(ids);
            super.delete(id);
        }
    }

    /**
     * 删除多条产品类型字典中的记录
     * @param id
     * @update 2022/2/12
     */
    @Override
    public void deleteBatch(Long[] ids) {
        for(Long id : ids) {
            delete(id);
        }
    }
}
